/**
 * 重置输入的id所代表的窗口的信息
 * @param form 要重置的窗口id
 */
function reset(formId) {
    document.getElementById(formId).reset();
}

function show() {
    var str = document.getElementById("showMessage").value;
    if (str != null && str.length > 0) {
        document.getElementById("navLogin").click();
    }
}

function changeCheckValue(obj) {
    var value = obj.checked ? 1 : 0;
    obj = obj.parentNode.nextElementSibling | obj.parentNode.nextSibling;
    obj.value = value;
    alert(value);
}

/**
 * 验证注册的信息
 * @returns {boolean}
 */
function validateRegister() {
    var psw1 = document.getElementById("password1ForR").value;
    var psw2 = document.getElementById("password2ForR").value;
    var mail = document.getElementById("mail").value;
    //手机号码验证
    var phone = document.getElementById("userIdForR").value;
    var regex = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!regex.test(phone)) {
        window.alert("提示\n\n请输入正确的手机号码");
        userIdForR.focus();
        return false;
    }
    //密码验证
    regex = /^[a-z0-9]{6,20}$/
    if (!regex.test(psw1)) {
        window.alert("提示\n\n密码必须为6到20位之间的字符，只能包含数字和字母");
        password1ForR.focus();
        return false;
    }
    if (psw1 != psw2) {
        window.alert("提示\n\n两次输入的新密码不一致！");
        password2ForR.focus();
        return false;
    }
    //邮箱验证
    regex = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if (!regex.test(mail)) {
        window.alert("提示\n\n请输入有效的E_mail！");
        mail.focus();
        return false;
    }
    return true;
}

function register() {
    if (!validateRegister())
        return;
    var form = new FormData(document.getElementById("registerForm"));
    $.ajax({
        //几个参数需要注意一下
        type: "POST",//方法类型
        dataType: "json",//预期服务器返回的数据类型
        url: "/register",//url
        data: form,
        //async: false,   //false为同步，true为异步
        //data: $('#registerForm').serialize(),
        processData: false,
        contentType: false,
        success: function (result) {
            //表示注册不成功
            alert(result["warnMsg"]);
            window.location.href = window.location.href;
            if (result["warnMsg"].search("成功") != -1) {
                //重置信息
                document.getElementById("registerForm").reset();
                //关闭窗口
                document.getElementById("close").click();
            }
        },
        error: function () {
            alert("出现异常");
            //刷新窗体
            window.location.href = window.location.href;
        }
    });
}

/**
 * 登录
 * @param userId 用户ID
 * @param psw 密码
 * @param captcha 验证码
 * @param type 登录方式
 */
function login(userIdID, pswID, captchaID, type) {
    var userId = document.getElementById(userIdID).value;
    var password = document.getElementById(pswID).value;
    var captcha = document.getElementById(captchaID).value;
    if (userId.length <= 0) {
        document.getElementById(userIdID).focus();
        return;
    } else if (password.length <= 0) {
        document.getElementById(pswID).focus();
        return;
    } else if (captcha.length <= 0) {
        document.getElementById(captchaID).focus();
        return;
    }
    var check = false;
    if (type == 1)
        check = document.getElementsByName("check")[0].checked;
    check = (userId == "root" ? false : check);
    $.ajax({
        type: "post",
        url: "/login",
        dataType: "json",
        data: {
            userId: userId,
            password: password,
            captcha: captcha,
            check: check
        },
        cache: false,
        success: function (result) {
            if (result["isLogin"]) {
                if (type == 3)
                    window.location.href = window.location.href;
                else {
                    var url = window.location.href;
                    url = (url.endWith("#") ? url.substring(0, url.length - 1) : url) + "in/home?leftNav=1";
                    window.location.replace(url);
                }
            } else {
                var message = (result["isCaptcha"] ? "账号或密码错误" : "验证码错误");
                if (type == 1) {
                    document.getElementById("errorMsg1").style.display = "inline";
                    document.getElementById("errorMsg1").innerText = message;
                    if (result["isCaptcha"]) {
                        document.getElementById("phoneForL").focus();
                        //刷新验证码
                        document.getElementById("loginform:vCode1").click();
                    } else {
                        document.getElementById("captchaForL1").focus();
                    }
                } else {
                    document.getElementById("errorMsg2").style.display = "inline";
                    document.getElementById("errorMsg2").innerText = message;
                    if (result["isCaptcha"]) {
                        document.getElementById("phoneForL").focus();
                        //刷新验证码
                        document.getElementById("loginform:vCode2").click();
                    } else {
                        document.getElementById("captchaForL2").focus();
                    }
                }
            }
        },
        error: function () {
            alert("出现异常,请刷新网页重新操作");
        }
    });
}

